//
//  HEMeViewController.m
//  huien
//
//  Created by yiban on 2017/5/26.
//  Copyright © 2017年 lcb. All rights reserved.
//

#import "HEMeViewController.h"
#import "UserInfoTableViewCell.h"
#import "UserMoneyTableViewCell.h"
#import "UserNewsTableViewCell.h"

#import "HEMemberViewController.h"
#import "HESettingViewController.h"
#import "HEMessageListViewController.h"
#import "HEMoneyViewController.h"
#import "UserLoanTableViewCell.h"
@interface HEMeViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    
}
@property(nonatomic,strong)LcbUITableView *lcbTableView;
@property(nonatomic,strong)MemberCenterModel *memberCenterModel;
@end

@implementation HEMeViewController
@synthesize lcbTableView=_lcbTableView;
@synthesize memberCenterModel=_memberCenterModel;
- (instancetype)init
{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(initAuthStatus:) name:@"authStatus" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tabChanage:) name:@"tabChanage222" object:nil];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navTitle = @"我的账户";
    [self initTableView];

}
-(void)tabChanage:(NSNotification*)notification
{
    if ([[NativeRouter sharedNativeRouter] isLogin:self]==NO) {
        return;
    }
}
-(void)initAuthStatus:(NSNotification*)notification
{
    NSLog(@"--------------->initAuthStatus:(NSNotification*)notification");
    [self reloadDataa];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if ([HEShare isLogin]==NO) {
        return;
    }
    [HEShare sharedHEShare].authStatus=0;
    [self reloadDataa];
    
}
-(void)dealloc
{
    if (_lcbTableView) {
        [_lcbTableView removeFromSuperview];
        _lcbTableView=nil;
    }
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [LcbLog log:@"HEMeViewController--------->dealloc"];
}
#pragma mark - tableview delegate
-(void)initTableView
{
    //
    UIButton *msgButton = [UIButton buttonWithType:UIButtonTypeCustom];
    msgButton.frame = CGRectMake(0, 20, 60, 44);
    msgButton.tag =100;
    [msgButton setImage:[UIImage imageNamed:@"message"] forState:UIControlStateNormal];
    [msgButton addTarget:self action:@selector(clickNavEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:msgButton];
    
    UIButton *setButton = [UIButton buttonWithType:UIButtonTypeCustom];
    setButton.frame = CGRectMake(SCREEN_WIDTH-60, 20, 60, 44);
    setButton.tag =101;
    [setButton setImage:[UIImage imageNamed:@"setting"] forState:UIControlStateNormal];
    [setButton addTarget:self action:@selector(clickNavEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:setButton];
    
    //
    if (_lcbTableView) {
        [_lcbTableView removeFromSuperview];
        _lcbTableView=nil;
    }
    _lcbTableView =[[LcbUITableView alloc] initWithFrame:CGRectMake(0, YB_HeaderHeight, SCREEN_WIDTH, SCREEN_HEIGHT-YB_HeaderHeight-50) style:UITableViewStylePlain];
    _lcbTableView.dataSource=self;
    _lcbTableView.delegate=self;
    _lcbTableView.backgroundColor=[UIColor whiteColor];
    _lcbTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_lcbTableView];
}
-(void)clickNavEvent:(id)sender
{
    UIButton *btn =(UIButton*)sender;
    switch (btn.tag) {
        case 100:
        {
            HEMessageListViewController *setting =[[HEMessageListViewController alloc] init];
            [self.navigationController pushViewController:setting animated:YES];
        }
            break;
        case 101:
        {
            HESettingViewController *setting =[[HESettingViewController alloc] init];
            [self.navigationController pushViewController:setting animated:YES];
        }
            break;
        default:
            break;
    }
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section==4)
    {
        return 4;
    }
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    switch (indexPath.section) {
        case 0:
        {
            return [UserInfoTableViewCell returnCellHeight];
        }
            break;
        case 1:
        {
            return [UserMoneyTableViewCell returnCellHeight];
        }
            break;
        case 2:
        {
            return [UserLoanTableViewCell returnCellHeight];
            
        }
            break;
        case 3:
        {
            return [UserNewsTableViewCell returnCellHeight];
            
        }
            break;
        case 4:
        {
            return 60;
        }
            break;
        
        default:
            break;
    }
    return 60;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.section) {
        case 0:
        {
            UserInfoTableViewCell  *userinfocell = [self.lcbTableView dequeueReusableCellWithIdentifier:@"UserInfoTableViewCell"];
            if (userinfocell == nil) {
                userinfocell = [[UserInfoTableViewCell  alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UserInfoTableViewCell"];
                userinfocell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            [userinfocell showInfo:self.memberCenterModel.member indexPath:indexPath];
            return userinfocell;
        }
            break;
        case 1:
        {
            UserMoneyTableViewCell  *usermoneycell = [self.lcbTableView dequeueReusableCellWithIdentifier:@"UserMoneyTableViewCell"];
            if (usermoneycell == nil) {
                usermoneycell = [[UserMoneyTableViewCell  alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UserMoneyTableViewCell"];
                usermoneycell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            [usermoneycell showInfo:self.memberCenterModel.fundlist indexPath:indexPath];
            return usermoneycell;
        }
            break;
        case 2:
        {
            UserLoanTableViewCell  *usermoneycell = [self.lcbTableView dequeueReusableCellWithIdentifier:@"UserLoanTableViewCell"];
            if (usermoneycell == nil) {
                usermoneycell = [[UserLoanTableViewCell  alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UserLoanTableViewCell"];
                usermoneycell.selectionStyle = UITableViewCellSelectionStyleNone;
                __weak typeof(self) weakSelf = self;
                [usermoneycell setCallBack:^(BEventType type, UIView *v, NSInteger index, id obj, NSIndexPath *path) {
                    if (type == BEventType_Module) {
                        ModuleModel *mmodel = nil;
                        for (ModuleModel *mudlemodel in weakSelf.memberCenterModel.moudle) {
                            if (mudlemodel.Id ==index) {
                                mmodel = mudlemodel;
                                break;
                            }
                        }
                        if (mmodel==nil) {
                            return;
                        }
                        [[NativeRouter sharedNativeRouter] nativeRouter:weakSelf params:mmodel completion:^(id obj) {
                            
                        }];
                    }
                }];
            }
            [usermoneycell showInfo:self.memberCenterModel.moudle indexPath:indexPath];
            return usermoneycell;
        }
            break;
        case 3:
        {
            UserNewsTableViewCell  *usernewscell = [self.lcbTableView dequeueReusableCellWithIdentifier:@"UserNewsTableViewCell"];
            if (usernewscell == nil) {
                usernewscell = [[UserNewsTableViewCell  alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UserNewsTableViewCell"];
                usernewscell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            [usernewscell showInfo:self.memberCenterModel.loans indexPath:indexPath];
            return usernewscell;
            
        }
            break;
        case 4:
        {
            UITableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
            if (cell == nil) {
                cell = [[UITableViewCell  alloc]initWithStyle:UITableViewCellStyleDefault   reuseIdentifier:@"UITableViewCell"];
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                UIView *line =[[UIView alloc] initWithFrame:CGRectMake(0, 59.5, SCREEN_WIDTH, 1)];
                line.backgroundColor=linecolor;
                [cell.contentView addSubview:line];
                line=nil;
                cell.textLabel.textColor = h2color;
                cell.textLabel.font =[UIFont systemFontOfSize:15];
            }
    
            switch (indexPath.row) {
                case 0:
                {
                    cell.imageView.image =[UIImage imageNamed:@"touzi_history"];
                    cell.textLabel.text=@"投资记录";
                }
                    break;
                case 1:
                {
                    cell.imageView.image =[UIImage imageNamed:@"zhuanrang_history"];
                    cell.textLabel.text=@"转让记录";
                }
                    break;
                case 2:
                {
                    cell.imageView.image =[UIImage imageNamed:@"jiekuan_history"];
                    cell.textLabel.text=@"借款记录";
                }
                    break;
                case 3:
                {
                    cell.imageView.image =[UIImage imageNamed:@"jiaoyi_history"];
                    cell.textLabel.text=@"交易记录";
                }
                    break;
                default:
                    break;
            }
            return cell;
            
        }
            break;
        
       
        default:
            break;
    }
    UITableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (cell == nil) {
        cell = [[UITableViewCell  alloc]initWithStyle:UITableViewCellStyleDefault   reuseIdentifier:@"UITableViewCell"];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
        {
            HEMemberViewController *setting =[[HEMemberViewController alloc] init];
            [self.navigationController pushViewController:setting animated:YES];
            
        }
            break;
        case 1:
        {
            HEMoneyViewController *meony =[[HEMoneyViewController alloc] init];
            [self.navigationController pushViewController:meony animated:YES];
        }
            break;
        case 2:
        {
        }
            break;
        case 3:
        {
            [[NativeRouter sharedNativeRouter] nativeRouter:self params:self.memberCenterModel.loans completion:^(id obj) {
                
            }];
        }
            break;
        case 4:
        {
            ModuleModel *mmodel = nil;
            for (ModuleModel *mudlemodel in self.memberCenterModel.moudle) {
                if (indexPath.row==0 && mudlemodel.Id ==4) {
                    mmodel = mudlemodel;
                    break;
                }else if (indexPath.row==1 && mudlemodel.Id ==5) {
                    mmodel = mudlemodel;
                    break;
                }else if (indexPath.row==2 && mudlemodel.Id ==6) {
                    mmodel = mudlemodel;
                    break;
                }else if (indexPath.row==3 && mudlemodel.Id ==7) {
                    mmodel = mudlemodel;
                    break;
                }
            }
            if (mmodel) {
                [[NativeRouter sharedNativeRouter] nativeRouter:self params:mmodel completion:^(id obj) {
                    
                }];
            }
         
        }
            break;
        default:
            break;
    }
    
}
-(void)reloadDataa
{
    SessionModel *sessionModel =[HEShare sessionModel];
    if (sessionModel==nil) {
        return;
    }
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    [dict setValue:sessionModel.sessionId forKey:@"sessionId"];
    [dict setValue:[NSString stringWithFormat:@"%ld",(long)sessionModel.session_user.userId] forKey:@"userId"];
    [dict setValue:sessionModel.session_user.realName forKey:@"username"];
    if ([HEShare sharedHEShare].authStatus==0) {
        [dict setValue:@"0" forKey:@"authStatus"];
    }else{
          [dict setValue:@"1" forKey:@"authStatus"];
    }
 
    __weak typeof(self) weakSelf = self;
    [HEApi memberCenterWithParam:dict serviceblock:^(id response, NSString *errorMsg) {
        if (response) {
            if ([response isKindOfClass:[LcbBaseModel class]]) {
                LcbBaseModel *baseModel = (LcbBaseModel*)response;
                [[NativeRouter sharedNativeRouter] codeHander:weakSelf params:baseModel completion:^(id obj) {
                    
                }];
            }else if ([response isKindOfClass:[MemberCenterModel class]]) {
                MemberCenterModel* tempModel =(MemberCenterModel*)response;
                weakSelf.memberCenterModel = tempModel;
                [weakSelf.lcbTableView reloadData];
            }
          
        }
        
    }];
}
@end
